#!/bin/bash

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
X11VNC_BIN="$SCRIPT_DIR/../bin/x11vnc"

if [ ! -f "$X11VNC_BIN" ]; then
    echo "错误: 找不到x11vnc二进制文件"
    exit 1
fi

chmod +x "$X11VNC_BIN"

echo "x11vnc 静态编译版本启动脚本"
echo "============================"

# 检查参数
if [ $# -eq 0 ]; then
    echo "用法: $0 [选项]"
    echo ""
    echo "选项:"
    echo "  basic     - 基本启动（默认）"
    echo "  password  - 设置密码启动"
    echo "  debug     - 调试模式启动"
    echo "  help      - 显示帮助"
    echo ""
    read -p "请选择启动模式 [basic]: " mode
    mode=${mode:-basic}
else
    mode=$1
fi

# 设置DISPLAY
if [ -z "$DISPLAY" ]; then
    export DISPLAY=:0
    echo "设置DISPLAY=$DISPLAY"
fi

case $mode in
    "basic")
        echo "启动x11vnc（基本模式）..."
        echo "连接方式: vncviewer <本机IP>:5900"
        echo "按Ctrl+C停止服务"
        exec "$X11VNC_BIN" -display "$DISPLAY" -rfbport 5900
        ;;
    "password")
        read -s -p "请输入VNC密码: " passwd
        echo
        echo "启动x11vnc（密码模式）..."
        echo "连接方式: vncviewer <本机IP>:5900"
        echo "密码: $passwd"
        echo "按Ctrl+C停止服务"
        exec "$X11VNC_BIN" -display "$DISPLAY" -rfbport 5900 -passwd "$passwd"
        ;;
    "debug")
        echo "启动x11vnc（调试模式）..."
        echo "日志文件: /tmp/x11vnc_debug.log"
        echo "按Ctrl+C停止服务"
        exec "$X11VNC_BIN" -display "$DISPLAY" -rfbport 5900 -debug -o /tmp/x11vnc_debug.log
        ;;
    "help"|"-h"|"--help")
        echo "x11vnc 启动脚本帮助"
        echo "=================="
        echo ""
        echo "启动模式:"
        echo "  basic     - 基本启动，无密码"
        echo "  password  - 设置密码启动"
        echo "  debug     - 调试模式，输出详细日志"
        echo ""
        echo "手动启动示例:"
        echo "  $X11VNC_BIN -display :0"
        echo "  $X11VNC_BIN -display :0 -passwd mypassword"
        echo "  $X11VNC_BIN -display :0 -rfbport 5901"
        echo ""
        echo "更多选项请参考: $X11VNC_BIN --help"
        ;;
    *)
        echo "未知模式: $mode"
        echo "使用 '$0 help' 查看帮助"
        exit 1
        ;;
esac
